<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>主题开发-语法 | Perfree</title>
    <meta name="description" content="">
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/assets/favicon.ico">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.js"></script>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.2/jquery.fancybox.min.css">
    <meta name="description" content="Java开发的建站平台">
    <link rel="preload" href="/assets/css/0.styles.53214819.css" as="style"><link rel="preload" href="/assets/js/app.94968a94.js" as="script"><link rel="preload" href="/assets/js/2.fdc7bca3.js" as="script"><link rel="preload" href="/assets/js/16.bb221be4.js" as="script"><link rel="prefetch" href="/assets/js/10.0ff33afc.js"><link rel="prefetch" href="/assets/js/11.c36dbcc6.js"><link rel="prefetch" href="/assets/js/12.92c9567c.js"><link rel="prefetch" href="/assets/js/13.c09257d0.js"><link rel="prefetch" href="/assets/js/14.715bbb46.js"><link rel="prefetch" href="/assets/js/15.e222f376.js"><link rel="prefetch" href="/assets/js/17.cda3dfb0.js"><link rel="prefetch" href="/assets/js/18.e2582183.js"><link rel="prefetch" href="/assets/js/3.f4d43266.js"><link rel="prefetch" href="/assets/js/4.e03bce9c.js"><link rel="prefetch" href="/assets/js/5.cf1c3294.js"><link rel="prefetch" href="/assets/js/6.d12ee77a.js"><link rel="prefetch" href="/assets/js/7.e7442db3.js"><link rel="prefetch" href="/assets/js/8.a73d51d6.js"><link rel="prefetch" href="/assets/js/9.169d2690.js">
    <link rel="stylesheet" href="/assets/css/0.styles.53214819.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="ant-row"><div class="sidebar-button"><i aria-label="icon: bars" class="anticon anticon-bars"><svg viewBox="0 0 1024 1024" focusable="false" data-icon="bars" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M912 192H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 284H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 284H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM104 228a56 56 0 1 0 112 0 56 56 0 1 0-112 0zm0 284a56 56 0 1 0 112 0 56 56 0 1 0-112 0zm0 284a56 56 0 1 0 112 0 56 56 0 1 0-112 0z"></path></svg></i> <span></span></div> <div class="ant-col ant-col-xs-24 ant-col-sm-24 ant-col-md-6 ant-col-lg-5 ant-col-xl-5 ant-col-xxl-4"><a href="/" class="router-link-active home-link"><img src="/assets/logo.png" alt="Perfree" class="logo"> <span class="site-name">Perfree</span></a> <div class="search-box mobile-search"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div></div> <div class="ant-col ant-col-xs-0 ant-col-sm-0 ant-col-md-18 ant-col-lg-19 ant-col-xl-19 ant-col-xxl-20"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><ul role="menu" id="nav" class="ant-menu ant-menu-horizontal ant-menu-root ant-menu-light"><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/" class="router-link-active">
          首页
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/doc/update-record.html">
          更新日志
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/doc/">
          文档
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/theme-download/">
          主题下载
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item ant-menu-item-selected"><a href="/theme-develop/" class="router-link-active">
          主题开发
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="visibility:hidden;position:absolute;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li></ul> <a href="https://github.com/perfree/PerfreeBlog" target="_blank" rel="noopener noreferrer" class="repo-link"><i aria-label="icon: github" class="anticon anticon-github"><svg viewBox="64 64 896 896" focusable="false" data-icon="github" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M511.6 76.3C264.3 76.2 64 276.4 64 523.5 64 718.9 189.3 885 363.8 946c23.5 5.9 19.9-10.8 19.9-22.2v-77.5c-135.7 15.9-141.2-73.9-150.3-88.9C215 726 171.5 718 184.5 703c30.9-15.9 62.4 4 98.9 57.9 26.4 39.1 77.9 32.5 104 26 5.7-23.5 17.9-44.5 34.7-60.8-140.6-25.2-199.2-111-199.2-213 0-49.5 16.3-95 48.3-131.7-20.4-60.5 1.9-112.3 4.9-120 58.1-5.2 118.5 41.6 123.2 45.3 33-8.9 70.7-13.6 112.9-13.6 42.4 0 80.2 4.9 113.5 13.9 11.3-8.6 67.3-48.8 121.3-43.9 2.9 7.7 24.7 58.3 5.5 118 32.4 36.8 48.9 82.7 48.9 132.3 0 102.2-59 188.1-200 212.9a127.5 127.5 0 0 1 38.1 91v112.5c.8 9 0 17.9 15 17.9 177.1-59.7 304.6-227 304.6-424.1 0-247.2-200.4-447.3-447.5-447.3z"></path></svg></i></a></nav></div></div> <!----></header> <aside class="sidebar"><!----> <ul class="sidebar-links"><li><a href="/theme-develop/" aria-current="page" title="准备" class="sidebar-link">准备</a></li><li><a href="/theme-develop/grammar.html" aria-current="page" title="模板语法" class="active sidebar-link">模板语法</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/theme-develop/grammar.html#简介" title="简介" class="sidebar-link">简介</a></li><li class="sidebar-sub-header"><a href="/theme-develop/grammar.html#输出" title="输出" class="sidebar-link">输出</a></li><li class="sidebar-sub-header"><a href="/theme-develop/grammar.html#安全输出" title="安全输出" class="sidebar-link">安全输出</a></li><li class="sidebar-sub-header"><a href="/theme-develop/grammar.html#表达式" title="表达式" class="sidebar-link">表达式</a></li><li class="sidebar-sub-header"><a href="/theme-develop/grammar.html#if判断" title="if判断" class="sidebar-link">if判断</a></li><li class="sidebar-sub-header"><a href="/theme-develop/grammar.html#switch判断" title="switch判断" class="sidebar-link">switch判断</a></li><li class="sidebar-sub-header"><a href="/theme-develop/grammar.html#for循环" title="for循环" class="sidebar-link">for循环</a></li><li class="sidebar-sub-header"><a href="/theme-develop/grammar.html#include包含" title="include包含" class="sidebar-link">include包含</a></li><li class="sidebar-sub-header"><a href="/theme-develop/grammar.html#更多" title="更多" class="sidebar-link">更多</a></li></ul></li><li><a href="/theme-develop/variable.html" title="全局变量" class="sidebar-link">全局变量</a></li><li><a href="/theme-develop/directive.html" title="自定义指令" class="sidebar-link">自定义指令</a></li><li><a href="/theme-develop/package.html" title="打包主题" class="sidebar-link">打包主题</a></li></ul></aside> <main class="page"> <div class="theme-antdocs-content content__default"><h2 id="简介"><a href="#简介" class="header-anchor">#</a> 简介</h2> <p>Perfree采用Jfinal 的 Enjouy模板引擎,在此之前使用了freemarker和thymeleaf模板引擎,最终的对比下,还是使用Enjoy模板引擎,该模板引擎具有极简设计、独创算法、极爽的开发体验,具体可查看官网介绍<a href="https://jfinal.com/doc/6-1" target="_blank" rel="noopener noreferrer">Jfinal Enjoy<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>,接下来将简单的介绍该模板引擎的语法,以便主题开发使用</p> <blockquote><p>详细文档请查看<a href="https://jfinal.com/doc/6-1" target="_blank" rel="noopener noreferrer">Jfinal Enjoy<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></blockquote> <h2 id="输出"><a href="#输出" class="header-anchor">#</a> 输出</h2> <p>所有的取值输出方式都是以#(key)的方式</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code>#(key)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token comment">&lt;!-- 输出SEO_TITLE --&gt;</span>
#(SEO_TITLE)
<span class="token comment">&lt;!-- 输出文章的标题 --&gt;</span>
#(article.title)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="安全输出"><a href="#安全输出" class="header-anchor">#</a> 安全输出</h2> <p>在开发过程中,可能会遇到某个值为空的情况,这个时候就需要使用安全输出</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code>#(key??)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>当然还可以指定当值为空的情况下输出定义的默认值</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code>#(key??'default')
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="表达式"><a href="#表达式" class="header-anchor">#</a> 表达式</h2> <p>表达式与java基本一致,以下摘抄至<a href="https://jfinal.com/doc/6-3" target="_blank" rel="noopener noreferrer">https://jfinal.com/doc/6-3<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <ul><li>算术运算： +   -   *   /   %   ++   --</li> <li>比较运算： &gt;  &gt;=   &lt;   &lt;=  ==   !=</li> <li>逻辑运算： !   &amp;&amp;   ||</li> <li>三元表达式： ? :</li> <li>Null 值常量: null</li> <li>字符串常量： &quot;jfinal club&quot;</li> <li>布尔常量：true false</li> <li>数字常量： 123  456F  789L  0.1D  0.2E10</li> <li>数组存取：array[i]</li> <li>属性取值：object.field</li> <li>方法调用：object.method(p1, p2…, pn) (支持可变参数)</li> <li>逗号表达式：123, 1&gt;2, null, &quot;abc&quot;, 3+6 (逗号表达式的值为最后一个表达式的值)</li></ul> <h2 id="if判断"><a href="#if判断" class="header-anchor">#</a> if判断</h2> <p>if指令需要一个 cond 表达式作为参数，并且以 #end 为结尾符，当 cond 求值为 true 时，执行 if 分支之中的代码</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code>#if(cond)
  ...
#end
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-html line-numbers-mode"><pre class="language-html"><code>#if(c1)
  ...
#else if(c2)
  ...
#else if (c3)
  ...
#else
  ...
#end
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h2 id="switch判断"><a href="#switch判断" class="header-anchor">#</a> switch判断</h2> <p>switch 指令和java 语言的 switch 语句基本用法一致</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code>#switch (month)
  #case (1, 3, 5, 7, 8, 10, 12)
    #(month) 月有 31 天
  #case (2)
    #(month) 月平年有28天，闰年有29天
  #default
    月份错误: #(month ?? &quot;null&quot;)
#end
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="for循环"><a href="#for循环" class="header-anchor">#</a> for循环</h2> <p>Enjoy Template Engine 对 for 指令进行了极为人性化的扩展，可以对任意类型数据进行迭代输出，包括支持 null 值迭代</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code>#for(x : list)
  #(x.field)
#end
 
#for(x : map)
  #(x.key)
  #(x.value)
#end

#for(x : listAaa)
   #(for.size)    被迭代对象的 size 值
   #(for.index)   从 0 开始的下标值
   #(for.count)   从 1 开始的记数值
   #(for.first)   是否为第一次迭代
   #(for.last)    是否为最后一次迭代
   #(for.odd)     是否为奇数次迭代
   #(for.even)    是否为偶数次迭代
   #(for.outer)   引用上层 #for 指令状态
#end
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>for 指令还支持 #else 分支语句,在for指令迭代次数为0时，将执行 #else 分支内部的语句</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code>#for(blog : blogList)
   #(blog.title)
#else
   您还没有写过博客，点击此处<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/blog/add<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>开博<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span>
#end
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="include包含"><a href="#include包含" class="header-anchor">#</a> include包含</h2> <p>include指令用于将外部模板内容包含进来，被包含的内容会被解析成为当前模板中的一部分进行使用</p> <div class="language-html line-numbers-mode"><pre class="language-html"><code>#include(&quot;sidebar.html&quot;)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="更多"><a href="#更多" class="header-anchor">#</a> 更多</h2> <blockquote><p>Jfinal Enjoy更多详细的语法指令请参考<a href="https://jfinal.com/doc/6-1" target="_blank" rel="noopener noreferrer">Jfinal Enjoy<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></blockquote></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated:</span> <span class="time">12/30/2020, 3:36:24 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/theme-develop/" class="prev router-link-active"><i aria-label="icon: left" class="anticon anticon-left"><svg viewBox="64 64 896 896" focusable="false" data-icon="left" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M724 218.3V141c0-6.7-7.7-10.4-12.9-6.3L260.3 486.8a31.86 31.86 0 0 0 0 50.3l450.8 352.1c5.3 4.1 12.9.4 12.9-6.3v-77.3c0-4.9-2.3-9.6-6.1-12.6l-360-281 360-281.1c3.8-3 6.1-7.7 6.1-12.6z"></path></svg></i>
        准备
      </a></span> <span class="next"><a href="/theme-develop/variable.html">
        全局变量
        <i aria-label="icon: right" class="anticon anticon-right"><svg viewBox="64 64 896 896" focusable="false" data-icon="right" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M765.7 486.8L314.9 134.7A7.97 7.97 0 0 0 302 141v77.3c0 4.9 2.3 9.6 6.1 12.6l360 281.1-360 281.1c-3.9 3-6.1 7.7-6.1 12.6V883c0 6.7 7.7 10.4 12.9 6.3l450.8-352.1a31.96 31.96 0 0 0 0-50.4z"></path></svg></i></a></span></p></div> </main> <!----></div><div class="global-ui"></div></div>
    <script src="/assets/js/app.94968a94.js" defer></script><script src="/assets/js/2.fdc7bca3.js" defer></script><script src="/assets/js/16.bb221be4.js" defer></script>
  </body>
</html>